package com.panopset.gui.container;

import static com.panopset.Util.EMPTY_STRING;
import static com.panopset.Util.dspmsg;
import static com.panopset.Util.isPopulated;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.JTextField;

import com.panopset.Alert;

public class PanTextField extends PanField<JTextField>
    implements KeyListener, FocusListener {
    protected final int cols;
    private final Vector<Validator> validators = new Vector<Validator>();

    public PanTextField(int cols, String borderTitle, String toolTipText) {
        this(cols, borderTitle, toolTipText, EMPTY_STRING);
    }

    public PanTextField(int cols, String borderTitle, String toolTipText, String defaultValue) {
        super(borderTitle, toolTipText, defaultValue);
        this.cols = cols;
    }

    /**
     * This won't do anything unless a validator has been added.
     * @param msg
     */
    protected String validate(String value) {
        String lastMessage = null;
        for (Validator validator : validators) {
            String msg = validator.validate(value);
            if (isPopulated(msg)) {
                lastMessage = msg;
            }
        }
        return lastMessage;
    }

    /**
     * Add a Validator to this field.
     * @param validator
     */
    public void addValidator(Validator validator) {
        validators.add(validator);
    }

    @Override
    public void setValue(String value) {
        String msg = validate(value);
        if (isPopulated(msg)) {
            dspHighlightedMsg(msg);
        } else {
            super.setValue(value);
        }
    }

    /**
     * Override to respond to a valid value entered by the user.
     * @param value
     */
    protected void update(String value) {

    }

    void reportChange() {
        resetHighlighting();
        String msg = validate(getInnerComponent().getText());
        if (isPopulated(msg)) {
            dspHighlightedMsg(msg);
        } else {
            resetHighlighting();
        }
    }

    protected void dspHighlightedMsg(String msg) {
        setAlertHighlighting();
        dspmsg(msg);
    }

    protected void resetHighlighting() {
        Alert.green(EMPTY_STRING);
        setNormalHighlighting();
    }

    @Override
    public String getComponentValue() {
        return getInnerComponent().getText();
    }

    @Override
    public void setComponentValue(String value) {
        getInnerComponent().setText(value);
    }

    @Override
    protected JTextField createComponent() {
        return new JTextField(cols);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // nothing to do here.
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            reportChange();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // nothing to do here.
    }

    @Override
    public void focusGained(FocusEvent e) {
        // nothing to do here.
    }

    @Override
    public void focusLost(FocusEvent e) {
        reportChange();
    }

    protected void init() {
        super.init();
        getInnerComponent().addFocusListener(this);
        getInnerComponent().addKeyListener(this);
    }
}
